package com.gyg.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Executors框架使用
 *
 * @author 郭运刚
 */
@Slf4j
public class ExecutorTest {

    public static void main(String[] args) {
        //创建固定大小的线程池
        //newFixedThreadPoolMethod();
        //创建带缓冲区的线程池
        //newCachedThreadPoolMethod();
        //synchronousQueue队列执行
        //synchronousQueueTestMethod();
        newSingleThreadExecutorMethod();

    }

    /**
     * 固定大小的线程池
     */
    public static void newFixedThreadPoolMethod() {
        //创建固定大小的线程池
        ExecutorService service = Executors.newFixedThreadPool(2);

/*        ExecutorService service = Executors.newFixedThreadPool(2, new ThreadFactory() {
            private AtomicInteger t = new AtomicInteger(1);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "MyPool_t" + t.getAndIncrement());
            }
        });*/

        //提交任务执行
        service.execute(() -> {
            log.info("1");
        });
        service.execute(() -> {
            log.info("2");
        });
        //加入阻塞队列
        service.execute(() -> {
            log.info("3");
        });
    }

    /**
     * 带缓冲区的线程池
     */
    public static void newCachedThreadPoolMethod() {
        ExecutorService service = Executors.newCachedThreadPool();
        //提交任务，创建三个救急线程执行
        service.execute(() -> {
            log.info("1");
        });
        service.execute(() -> {
            log.info("2");
        });
        service.execute(() -> {
            log.info("3");
        });

    }

    /**
     * SynchronousQueue队列提交执行演示
     */
    public static void synchronousQueueTestMethod() {
        SynchronousQueue<Integer> sq = new SynchronousQueue<>();
        //通过一个线程向队列中提交任务
        new Thread(() -> {
            try {
                log.info("开始提交任务1");
                sq.put(1);
                log.info("执行完毕任务1");

                log.info("开始提交任务2");
                sq.put(2);
                log.info("执行完毕任务2");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t1").start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //从队列中获取任务
        new Thread(() -> {
            try {
                log.info("获取线程1");
                sq.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2").start();

        new Thread(() -> {
            try {
                log.info("获取线程2");
                sq.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t3").start();
    }

    /**
     * 单线程线程池
     */
    public static void newSingleThreadExecutorMethod() {
        ExecutorService service = Executors.newSingleThreadExecutor();
        //提交三个任务执行
        //任务1执行异常
        service.execute(() -> {
            log.info("1");
            int i = 1 / 0;
        });

        service.execute(() -> {
            log.info("2");
        });

        service.execute(() -> {
            log.info("3");
        });
    }
}
